/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.力扣中级算法.扁平化嵌套列表迭代器;

import org.python.antlr.runtime.DFA;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

interface NestedInteger {

    // @return true if this NestedInteger holds a single integer, rather than a nested list.
    public boolean isInteger();

    // @return the single integer that this NestedInteger holds, if it holds a single integer
    // Return null if this NestedInteger holds a nested list
    public Integer getInteger();

    // @return the nested list that this NestedInteger holds, if it holds a nested list
    // Return empty list if this NestedInteger holds a single integer
    public List<NestedInteger> getList();
}

public class NestedIterator implements Iterator<Integer> {
    LinkedList<Integer> data = new LinkedList<>();

    public NestedIterator(List<NestedInteger> nestedList) {
        init(nestedList);
    }

    private void init(List<NestedInteger> nestedList) {
        for (NestedInteger nestedInteger : nestedList) {
            if (nestedInteger.isInteger()) {
                data.addLast(nestedInteger.getInteger());
            } else
                init(nestedInteger.getList());
        }
    }

    @Override
    public Integer next() {

        return data.pollFirst();
    }

    @Override
    public boolean hasNext() {

        return !data.isEmpty();
    }
}